package cn.cscamp.middleware.loadbalancer.strategy;

import java.util.Collections;
import java.util.List;

public class RandomLoadbalancer {

    private final List<String> serviceAddressList;

    private final int retry;

    public RandomLoadbalancer(List<String> serviceAddressList) {
        Collections.shuffle(serviceAddressList);
        this.serviceAddressList = serviceAddressList;
        this.retry = serviceAddressList.size() - 1;
    }

    public String chooseOne() {
        return serviceAddressList.get(0);
    }

    public synchronized void onError() {
        serviceAddressList.remove(0);
    }

    public int getRetry() {
        return retry;
    }
}
